package top.dfghhj.chapter02;

/**
 * @Author: feifeng
 * @Description: 对象进入老年代
 * @Date: 2021/4/8 12:16
 */
public class JvmTest {

    /**
     * JVM 参数:
     * -XX:NewSize=10485760 -XX:MaxNewSize=10485760 -XX:InitialHeapSize=20971520 -XX:MaxHeapSize=20971520 -XX:SurvivorRatio=8  -XX:MaxTenuringThreshold=15 -XX:PretenureSizeThreshold=10485760 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:gc.log
     * @param args
     */
    public static void main(String[] args) {
        // 动态年龄判断
//        test1();

        // 满年龄15岁 进入老年代
        test2();
    }

    /**
     * 动态年龄判断
     *
     * 年龄1 + 年龄2 + 年龄3 + ... + 年龄n 总大小大于survivor的50%，大于n的就会移到老年代
     *
     * Java HotSpot(TM) 64-Bit Server VM (25.281-b09) for windows-amd64 JRE (1.8.0_281-b09), built on Dec  9 2020 13:54:07 by "" with MS VC++ 15.9 (VS2017)
     * Memory: 4k page, physical 16571704k(9062012k free), swap 19062072k(10605696k free)
     * CommandLine flags: -XX:InitialHeapSize=20971520 -XX:MaxHeapSize=20971520 -XX:MaxNewSize=10485760 -XX:MaxTenuringThreshold=15 -XX:NewSize=10485760 -XX:OldPLABSize=16 -XX:PretenureSizeThreshold=10485760 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:SurvivorRatio=8 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:-UseLargePagesIndividualAllocation -XX:+UseParNewGC
     * 0.075: [GC (Allocation Failure) 0.075: [ParNew: 7420K->790K(9216K), 0.0014452 secs] 7420K->790K(19456K), 0.0015588 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.077: [GC (Allocation Failure) 0.077: [ParNew: 7095K->0K(9216K), 0.0025265 secs] 7095K->776K(19456K), 0.0025621 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * Heap
     *  par new generation   total 9216K, used 2212K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
     *   eden space 8192K,  27% used [0x00000000fec00000, 0x00000000fee290e0, 0x00000000ff400000)
     *   from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
     *   to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
     *  concurrent mark-sweep generation total 10240K, used 776K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
     *  Metaspace       used 2664K, capacity 4486K, committed 4864K, reserved 1056768K
     *   class space    used 283K, capacity 386K, committed 512K, reserved 1048576K
     *
     */
    public static void test1() {
        byte[] array1 = new byte[2 * 1024 * 1024];
        array1 = new byte[2 * 1024 * 1024];
        array1 = new byte[2 * 1024 * 1024];
        array1 = null;

        byte[] array2 = new byte[128 * 1024];

        byte[] array3 = new byte[2 * 1024 * 1024];
        array3 = new byte[2 * 1024 * 1024];
        array3 = new byte[2 * 1024 * 1024];
        array3 = new byte[128 * 1024];
        array3 = null;

        byte[] array4 = new byte[2 * 1024 * 1024];
    }

    /**
     *
     * 模拟对象年龄大于15，移入老年代
     *
     * JVM 参数调整： NewSize 改成12M， SurvivorRatio改成4，这样eden就有8MB，s0和s1各有2M
     * -XX:NewSize=12582912 -XX:MaxNewSize=12582912 -XX:InitialHeapSize=20971520 -XX:MaxHeapSize=20971520 -XX:SurvivorRatio=4  -XX:MaxTenuringThreshold=15 -XX:PretenureSizeThreshold=10485760 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:gc.log
     *
     * 分析：
     * 在循环外创建128k大小的数组
     * 第1次循环，把eden区塞满，
     * 第2次循环，创建第一个2M大小的数组时都会触发minor gc，把前一次在循环中创建的对象都回收掉
     * 。。。
     * 直到第16次循环，执行第15次minor gc之后，在循环外创建的数组因为躲过了15次gc，年龄达到15，被移动到了老年代
     *
     * GC log :
     * Java HotSpot(TM) 64-Bit Server VM (25.281-b09) for windows-amd64 JRE (1.8.0_281-b09), built on Dec  9 2020 13:54:07 by "" with MS VC++ 15.9 (VS2017)
     * Memory: 4k page, physical 16571704k(8558168k free), swap 19062072k(9414240k free)
     * CommandLine flags: -XX:InitialHeapSize=20971520 -XX:MaxHeapSize=20971520 -XX:MaxNewSize=12582912 -XX:MaxTenuringThreshold=15 -XX:NewSize=12582912 -XX:OldPLABSize=16 -XX:PretenureSizeThreshold=10485760 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:SurvivorRatio=4 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:-UseLargePagesIndividualAllocation -XX:+UseParNewGC
     * 0.063: [GC (Allocation Failure) 0.063: [ParNew: 7420K->891K(10240K), 0.0010989 secs] 7420K->891K(18432K), 0.0011933 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.064: [GC (Allocation Failure) 0.064: [ParNew: 8220K->1055K(10240K), 0.0005542 secs] 8220K->1055K(18432K), 0.0005838 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.065: [GC (Allocation Failure) 0.065: [ParNew: 8368K->991K(10240K), 0.0007785 secs] 8368K->991K(18432K), 0.0008130 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.066: [GC (Allocation Failure) 0.066: [ParNew: 8309K->868K(10240K), 0.0005529 secs] 8309K->868K(18432K), 0.0005815 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.067: [GC (Allocation Failure) 0.067: [ParNew: 8190K->1001K(10240K), 0.0010310 secs] 8190K->1001K(18432K), 0.0010576 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.069: [GC (Allocation Failure) 0.069: [ParNew: 8326K->905K(10240K), 0.0006589 secs] 8326K->905K(18432K), 0.0006865 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.070: [GC (Allocation Failure) 0.070: [ParNew: 8231K->953K(10240K), 0.0007910 secs] 8231K->953K(18432K), 0.0008186 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.071: [GC (Allocation Failure) 0.071: [ParNew: 8280K->893K(10240K), 0.0009431 secs] 8280K->893K(18432K), 0.0009710 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.072: [GC (Allocation Failure) 0.072: [ParNew: 8220K->1001K(10240K), 0.0010108 secs] 8220K->1001K(18432K), 0.0010385 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.073: [GC (Allocation Failure) 0.073: [ParNew: 8328K->1144K(10240K), 0.0004240 secs] 8328K->1144K(18432K), 0.0004632 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.074: [GC (Allocation Failure) 0.074: [ParNew: 8472K->1083K(10240K), 0.0010426 secs] 8472K->1083K(18432K), 0.0010720 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.076: [GC (Allocation Failure) 0.076: [ParNew: 8411K->989K(10240K), 0.0005218 secs] 8411K->989K(18432K), 0.0005506 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.076: [GC (Allocation Failure) 0.076: [ParNew: 8317K->1066K(10240K), 0.0007612 secs] 8317K->1066K(18432K), 0.0007850 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.078: [GC (Allocation Failure) 0.078: [ParNew: 8394K->965K(10240K), 0.0008064 secs] 8394K->965K(18432K), 0.0008261 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.079: [GC (Allocation Failure) 0.079: [ParNew: 8293K->902K(10240K), 0.0004689 secs] 8293K->902K(18432K), 0.0004920 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.079: [GC (Allocation Failure) 0.079: [ParNew: 8230K->0K(10240K), 0.0025053 secs] 8230K->776K(18432K), 0.0025314 secs] [Times: user=0.05 sys=0.00, real=0.02 secs]
     * Heap
     *  par new generation   total 10240K, used 1188K [0x00000000fec00000, 0x00000000ff800000, 0x00000000ff800000)
     *   eden space 8192K,  14% used [0x00000000fec00000, 0x00000000fed290e0, 0x00000000ff400000)
     *   from space 2048K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff600000)
     *   to   space 2048K,   0% used [0x00000000ff600000, 0x00000000ff600000, 0x00000000ff800000)
     *  concurrent mark-sweep generation total 8192K, used 776K [0x00000000ff800000, 0x0000000100000000, 0x0000000100000000)
     *  Metaspace       used 2664K, capacity 4486K, committed 4864K, reserved 1056768K
     *   class space    used 283K, capacity 386K, committed 512K, reserved 1048576K
     *
     */
    public static void test2() {
        byte[] array1 = new byte[128 * 1024];
        for (int i = 0; i <= 15; i++) {
            byte[] array2 = new byte[2 * 1024 * 1024];
            array2 = new byte[2 * 1024 * 1024];
            array2 = new byte[2 * 1024 * 1024];
            array2 = null;
            byte[] array3 = new byte[1 * 1024 * 1024];
        }

//        // 儒猿同学写法
//        int age = 16;
//        byte[] array1 = new byte[128 * 1024];
//        while (age >= 0 ) {
//            byte[] array2 = new byte[2 * 1024 * 1024];
//            array2 = new byte[1 * 1024 * 1024];
//            array2 = new byte[1 * 1024 * 1024];
//            array2 = null;
//            byte[] array3 = new byte[2 * 1024 * 1024];
//            age--;
//        }
    }
}
